package com.tuanzi.loan.business.service.receipt.user.impl;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tuanzi.loan.business.constants.DictionaryCode;
import com.tuanzi.loan.business.entity.receipt.user.ReceiptSystemUser;
import com.tuanzi.loan.business.entity.receipt.user.ReceiptSystemUserStatus;
import com.tuanzi.loan.business.repository.receipt.user.ReceiptSystemUserRepository;
import com.tuanzi.loan.business.service.receipt.user.ReceiptSystemUserService;
import com.tuanzi.loan.business.service.system.DictionaryService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.receipt.user.ReceiptSystemUserSearchRequest;
import com.tuanzi.loan.vo.response.receipt.user.ReceiptSystemUserSearchResponse;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Ray 2017/09/21
 */
@Debug
@Slf4j
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class ReceiptSystemUserServiceImpl extends BaseAuditServiceImpl<ReceiptSystemUser, String> implements ReceiptSystemUserService {

	@Autowired
	private ReceiptSystemUserRepository receiptSystemUserRepository;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private PasswordEncoder passwordEncoder;

	@Override
	protected JPAAuditRepository<ReceiptSystemUser, String> getRepository() {
		return receiptSystemUserRepository;
	}

	@Override
	public Page<ReceiptSystemUserSearchResponse> list(PageSearcher<ReceiptSystemUserSearchRequest> pageSearcher) {
		return receiptSystemUserRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
	}

	private String getDefaultPassword() {
		String defaultPwd = dictionaryService.findByCode(DictionaryCode.RECEIPT_DEFAULT_USER_PWD).getValue1();
		return passwordEncoder.encode(defaultPwd);
	}

	@Override
	public List<ReceiptSystemUser> disable(List<ReceiptSystemUser> receiptSystemUsers) {
		return receiptSystemUsers.parallelStream().map(this::disable).collect(Collectors.toList());
	}

	@Override
	public ReceiptSystemUser disable(ReceiptSystemUser receiptSystemUser) {
		receiptSystemUser.setStatus(ReceiptSystemUserStatus.DISABLED);
		return saveOrUpdate(receiptSystemUser);
	}

	@Override
	public List<ReceiptSystemUser> enable(List<ReceiptSystemUser> receiptSystemUsers) {
		return receiptSystemUsers.parallelStream().map(this::enable).collect(Collectors.toList());
	}

	@Override
	public ReceiptSystemUser enable(ReceiptSystemUser receiptSystemUser) {
		receiptSystemUser.setStatus(ReceiptSystemUserStatus.NORMAL);
		return saveOrUpdate(receiptSystemUser);
	}

	@Override
	public void unlock(String id) {
		ReceiptSystemUser receiptSystemUser = receiptSystemUserRepository.findOne(id);
		receiptSystemUser.setStatus(ReceiptSystemUserStatus.NORMAL);
		saveOrUpdate(receiptSystemUser);
	}

	@Override
	public void approve(String id) {
		ReceiptSystemUser receiptSystemUser = receiptSystemUserRepository.findOne(id);
		receiptSystemUser.setStatus(ReceiptSystemUserStatus.NORMAL);
		receiptSystemUserRepository.save(receiptSystemUser);
	}

	@Override
	public void resetReceiptPassword(String userId) {
		ReceiptSystemUser user = receiptSystemUserRepository.findOne(userId);
		user.setPwd(getReceiptDefaultPassword());
		this.saveOrUpdate(user);
	}

	private String getReceiptDefaultPassword() {
		String defaultPwd = dictionaryService.findByCode(DictionaryCode.RECEIPT_DEFAULT_USER_PWD).getValue1();
		return passwordEncoder.encode(defaultPwd);
	}

	@Override
	public List<ReceiptSystemUser> findByParentIsNull() {
		return receiptSystemUserRepository.findByParentIsNull();
	}

	@Override
	public void reject(String id) {
		ReceiptSystemUser receiptSystemUser = receiptSystemUserRepository.findOne(id);
		receiptSystemUser.setStatus(ReceiptSystemUserStatus.FAIL);
		saveOrUpdate(receiptSystemUser);
	}
}
